//package com.lry.sso.auth.server.filter;
//
//import com.lry.sso.auth.common.bean.RequestContext;
//import com.lry.sso.auth.server.config.AuthConfigProperties;
//import com.lry.sso.auth.common.util.SessionInfo;
//import com.lry.sso.exception.SsoErrorEnum;
//import com.lry.sso.common.exception.SysInvocationException;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.util.StringUtils;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * 验证处理
// *
// * @author 李瑞益
// * @date 2019/12/7
// */
//public class RequestHandler implements HandlerInterceptor {
//
//    private static Logger logger = LoggerFactory.getLogger(RequestHandler.class);
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String requestUri = request.getRequestURI();
//        RequestContext.setUrlSession(AuthUtil.getUrlSession(request));
//
//        List<String> openUrl = new ArrayList<>();
//        //接口的验证放表里
////        SysMenuMapper sysMenuMapper = SpringUtil.getBean(SysMenuMapper.class);
////
////        if (sysMenuMapper != null) {
////            SysMenu menu = new SysMenu();
////            menu.setMenuType(LoginAuthorizationConstant.MENU_TYPE);
////            List<SysMenu> settingList = sysMenuMapper.listWithParam(menu);
////            if (settingList != null && settingList.size() > 0) {
////                openUrl.addAll(settingList.stream().map(SysMenu::getUrl).collect(Collectors.toList()));
////            }
////        }else{
////            logger.warn("警告：sysSettingService对象不存在，无法添加入库配置！！！");
////        }
////        //放通路径；配置的url应该是匹配的不是精确的。
//        boolean isVerify = false;
////        for (String url : openUrl) {
////            if (requestUri.indexOf(url) != -1) {
////                isVerify = true;
////            }
////        }
//        //白名单；防止配置错误被拦截
//        List<String> whiteList = new ArrayList<>();
//        whiteList.add("csc/system/v1/login");
//        whiteList.add("csc/system/v1/user/getPublicKey");
//        whiteList.add("csc/system/v1/captcha");
//        whiteList.add("csc/system/v1/captcha/validate");
//
//        for (String white : whiteList) {
//            if (requestUri.indexOf(white) != -1) {
//                isVerify = false;
//            }
//        }
//        if (!isVerify) {
//            return true;
//        }
//
//        AuthConfigProperties bean = SpringUtil.getBean(AuthConfigProperties.class);
//        if (!bean.getIsAuth()) {
//            return true;
//        }
//        // 清除token
//        RequestContext.setToken(null);
//        RequestContext.set(null);
//
//        //token 解析
//        String tokens = AuthUtil.getTokenForUrl(request);
//        if (StringUtils.isEmpty(tokens)) {
//            logger.info("无认证信息token：{}", tokens);
//            throw new SysInvocationException(SsoErrorEnum.TOKEN_UNABLE.code(), "无认证信息token");
//        }
//
//        SessionInfo sessionInfo = AuthUtil.getSessionInfo(tokens);
//        logger.info("token 信息：{}", sessionInfo.toString());
//        //判断redis中是否存在token，验证是否注销
//        RedisService redisService = SpringUtil.getBean(RedisService.class);
//        logger.info("判断用户是否注销。。。");
//        String key = AuthUtil.getLoginJwtKey(sessionInfo.getUserName());
//        String s = redisService.get(key);
//        if (!tokens.equals(s) || StringUtils.isEmpty(s)) {
//            //注销用户
//            logger.info("无效的token ，数据库无token信息或token值被覆盖，请检查改账号是否被注销或其他地方登录。");
//            throw new SysInvocationException(SsoErrorEnum.TOKEN_EXPIRE.code(), "登录信息失效或该账号其他地方登录，请重新登录");
//        }
////            else {
////                //续存；毫秒
////                Long expire = null;
////                try {
////                    expire = jwtTokenService.getExpire(tokens);
////                } catch (Exception e) {
////                    logger.info("获取token过期时间异常：{}", e.getMessage());
////                    e.printStackTrace();
////                }
////                logger.info("token过期时间剩余：{}秒",expire/1000.0);
////                if (expire < bean.getLoginExpire()) {
////                    //token剩余时间
////                    logger.info("token过期时间小于登录过期时间，设置token过期时间为登录过期时间。");
////                    jedis.expire(key,(int)(expire/1000));
////                }else{
////                    Long time = jedis.pttl(key);
////                    Long seconds = time / 1000;
////                    if (seconds < bean.getRenewExpire()) {
////                        //30分钟
////                        logger.info("登录过期时间小于刷新时间;重置登录过期时间为：{}秒",bean.getLoginExpire());
////                        jedis.expire(key,bean.getLoginExpire());
////                    }
////                }
////            }
//        response.setHeader("test-header", "test-header");
//        if (sessionInfo != null) {
//            sessionInfo.setUrl(request.getRequestURL().toString());
//        }
//        RequestContext.setToken(tokens);
//        RequestContext.set(sessionInfo);
//        return true;
//    }
//}
